package com.datastructure.practice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TotalOfThreeNumber {

}
class Solution2 {
    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> data = new ArrayList<>();
        int length = nums.length;
        int sum =0;
        if(nums==null||length<3){
            return data;
        }
        Arrays.sort(nums);
        for(int i = 0;i < length;i++){
            if(nums[i]>0) break;
            if(i > 0 && nums[i]==nums[i-1]) continue;
            int L = i + 1;
            int R = length - 1;
            while (L<R){
                sum = nums[i]+nums[L]+nums[R];
                if(sum==0){
                    data.add(Arrays.asList(nums[i],nums[L],nums[R]));
                    while (L<R && nums[L] == nums[L+1]) L++; // 去重
                    while (L<R && nums[R] == nums[R-1]) R--; // 去重
                    L++;
                    R--;
                } else if(sum<0){
                    L++;
                } else if(sum>0){
                    R--;
                }
            }
        }
        return data;
    }
}
